import { Effect, Reducer } from 'umi';
import {
  _getExamType,
  _getQuestionsType,
  _getSubject,
  _getQuestionsList,
  _getQuestionsCondition,
  _updateQuestion,
} from '@/api/watchQuestion';
import { message } from 'antd';
import {
  Exam_Type,
  Questions_Type,
  Subject_Item,
  Questions_List_Item,
} from '@/type/watchQuestion';
import updataState from '@/utils/modelPut';
export interface WatchQuestionModelState {
  examTypeList: Exam_Type[];
  questionsType: Questions_Type[];
  subjectList: Subject_Item[];
  questionList: Questions_List_Item[];
  detail: Questions_List_Item | {};
}
export interface WatchQuestionModelType {
  namespace: 'watchQuestion';
  state: WatchQuestionModelState;
  effects: {
    GET_EXAM_TYPE: Effect;
    GET_QUESTION_TYPE: Effect;
    GET_SUBJECT_LIST: Effect;
    GET_QUESTION_LIST: Effect;
    GET_QUESTION_CONDITION: Effect;
    GET_DETAIL_QUESTION: Effect;
    UPDATE_QUESTION: Effect;
  };
  reducers: {
    save: Reducer<WatchQuestionModelState>;
  };
}
const WatchQuestionModel: WatchQuestionModelType = {
  namespace: 'watchQuestion',
  state: {
    examTypeList: [],
    questionsType: [],
    subjectList: [],
    questionList: [],
    detail: {},
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
  effects: {
    //考试类型接口
    *GET_EXAM_TYPE({}, { call, put }) {
      let res = yield call(_getExamType);
      //用户信息获取成功
      if (res.data.code == 1) {
        yield put(updataState('save', { examTypeList: res.data.data }));
      } else {
        message.error(res.data.msg);
      }
    },
    //试题类型接口
    *GET_QUESTION_TYPE({}, { call, put }) {
      let res = yield call(_getQuestionsType);
      //用户信息获取成功
      if (res.data.code == 1) {
        yield put(updataState('save', { questionsType: res.data.data }));
      } else {
        message.error(res.data.msg);
      }
    },
    //课程接口
    *GET_SUBJECT_LIST({}, { call, put }) {
      let res = yield call(_getSubject);
      //用户信息获取成功
      if (res.data.code == 1) {
        yield put(updataState('save', { subjectList: res.data.data }));
      } else {
        message.error(res.data.msg);
      }
    },
    //获取所有试题
    *GET_QUESTION_LIST({}, { call, put }) {
      let res = yield call(_getQuestionsList);
      //用户信息获取成功
      if (res.data.code == 1) {
        yield put(updataState('save', { questionList: res.data.data }));
      } else {
        message.error(res.data.msg);
      }
    },
    //获取详情试题
    *GET_DETAIL_QUESTION({ payload }, { call, put }) {
      let res = yield call(_getQuestionsList);
      let detail = res.data.data.find(
        (item: Questions_List_Item) => payload == item.questions_id,
      );
      //用户信息获取成功
      if (res.data.code == 1) {
        yield put(updataState('save', { questionList: res.data.data, detail }));
      } else {
        message.error(res.data.msg);
      }
    },
    //按条件获取所有试题
    *GET_QUESTION_CONDITION({ payload }, { call, put }) {
      let res = yield call(_getQuestionsCondition, payload);
      //用户信息获取成功
      if (res.data.code == 1) {
        yield put(updataState('save', { questionList: res.data.data }));
      } else {
        message.error(res.data.msg);
      }
    },
    //按条件获取所有试题
    *UPDATE_QUESTION({ payload }, { call, put }) {
      console.log(payload);
      let res = yield call(_updateQuestion, payload);
      console.log(res);
      
      //用户信息获取成功
      if (res.data.code == 1) {
        console.log(res.data.code);
        
        // message.success(res.data.msg);
      } else {
        message.error(res.data.msg);
      }
    },
  },
};

export default WatchQuestionModel;
